package cn.tedu.myweb.controller;

import cn.tedu.myweb.exception.ServiceException;
import cn.tedu.myweb.service.IChatsService;
import cn.tedu.myweb.vo.ChatVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/chats")
@Slf4j
public class ChatController {
    @Autowired
    IChatsService chatsService;

    @PostMapping("")
    public String saveChatHistory(ChatVo chatVo,
                                @AuthenticationPrincipal UserDetails user){
        log.debug("接收到的用户信息{}",chatVo);

        try {
            chatsService.saveChatHistory(chatVo,user.getUsername());
            return "OK";
        } catch (ServiceException e) {
            log.error("发送失败！");
            return e.getMessage();
        }

    }
}
